<?php

/* Title: Customer Classes - Class for managing user information
   Author: Rahl Genesis  <coder.chuck@gmail.com>
   Release: G.O.A.T. - Group Object Assisted Transfer
   Version: 0.0.1
*/

class Customer {

protected $dbUserName = "test";
protected $dbPass = "test123";
protected $dbHost = "localhost";
protected $dbDatabase = "test";
protected $dbTable = "customers";

public $key;
public $firstname;
public $lastname;

protected $DEBUG = true;

/* Start off with some error checking.
	To Do: Validation of Data prior to input.
	Data Formatting 
*/


public function setVars($uuid, $slFname, $slLname) {
	
	$this->key = $uuid;
	$this->firstname = $slFname;
	$this->lastname = $slLname;
	
	if ($DEBUG) {
		
	echo "setting key to:". $this->key;
	echo "<br>setting firstname to:". $this->firstname;
	echo "<br>setting lastname to:". $this->lastname;
	echo "<br>";
	}
}

public function checkVars() {
	if (!$this->key) { return false; }
	if (!$this->firstname) {  return false;}
	if (!$this->lastname) { return false;}
	
	return true;
}


/* Onward to doing the Database Functions */

public function checkUserExistance() {
	if (!$this->key) {
		return "missing_key";
	}
	
	$dbh = mysqli_connect($this->dbhost, $this->dbUserName, $this->dbPass, $this->dbDatabase);
	if (!$dbh)
    	{
        //die("Not connected : " . mysqli_error($dbh));
	return "db_connect_failure";
    	}
    	
    	
        $query = "SELECT * FROM `$this->dbDatabase`.`$this->dbTable` WHERE customer_key = '" .$this->key ."'";
        $result = mysqli_query($dbh,$query);
        
	if (!$result)
        {
		
	        return "key_not_found"; 
        }
	return "key_found";
	
}

public function customerRegister() {


	$variablesPresent = $this->checkVars(); 	
	if (!$variablesPresent) {
		if ($DEBUG) {
		echo $this->key;
		echo "<p>", $this->firstname;
		echo "<p>", $this->lastname;
		}
		return "missing_arguments"; 
	}


	$dbh = mysqli_connect($this->dbhost, $this->dbUserName, $this->dbPass, $this->dbDatabase);
	if (!$dbh)
    	{
        //die("Not connected : " . mysqli_error($dbh));
	return "db_connect_failure";
    	}
    	
    	
        $query = "SELECT * FROM `$this->dbTable` WHERE customer_key = '" .$this->key ."'";
        $result = mysqli_query($dbh,$query);
        
	if (!$result)
        {
		if ($DEBUG) { echo mysqli_error($dbh); } //-- May be useful for later debugging (perhaps a LOG file?)
	        return "result_failure"; 
        }
        
            $row = mysqli_fetch_row($result);
            if ($row >= 1)
            {
                return "key_in_list";
            }

                $query = "
                        INSERT INTO `$this->dbDatabase`.`$this->dbTable` (
                        `first_name` ,
                        `last_name` ,
                        `customer_key` 
                        )
                        VALUES (
                        '$this->firstname','$this->lastname','$this->key'
                        )";
		
			if ($DEBUG) {
			echo "Running Query with: ";
			echo $query;
			echo "<br>";
			}
		
                $result = mysqli_query($dbh,$query);
		// better close the connection before we return
		               
		if (!$result) {
			if ($DEBUG) { echo mysqli_error($dbh); }
			mysqli_close($dbh); 
			return ("new_key_failed");
			
		} else {
			mysqli_close($dbh); 
		return ("new_key_added");
		}
      
	} // end of customerRegister
	
	

public function customerRemove () {

$dbh = mysqli_connect($this->dbhost, $this->dbUserName, $this->dbPass, $this->dbDatabase);
    if (!$dbh)
    	{
        //die("Not connected : " . mysqli_error($dbh));
	return "db_connect_failure";
    	}

        $query = "SELECT * FROM `$this->dbDatabase`.`$this->dbTable` WHERE customer_key = '$this->key'";
        $result = mysqli_query($dbh,$query);
	if (!$result)
        {
         //   echo 'Could not run query: ' . mysqli_error($dbh);
	  return "result_failure";
        }
       
	    $row = mysqli_fetch_row($result);
            
            if ($row >= 1)
            {
                $query = "DELETE FROM `$this->dbDatabase`.`$this->dbTable` WHERE customer_key = '$this->key'";
                        
                $result = mysqli_query($dbh,$query);
		
		if (!$result) {
			if ($DEBUG) { echo mysqli_error($dbh); }
			mysqli_close($dbh); 
			return ("delete_key_failed");
			
		} else {
			mysqli_close($dbh); 
		return ("key_removed");
		}
		
            }
            else
            {
                return"key_not_in_list";                    
            }
        
        mysqli_close($dbh);                        


} // end of customerRemove


/* COMMENTED OUT FOR QUICK PUSH
  
private function getCustomerList($orderby) {

	$dbh = mysqli_connect($this->dbhost, $this->dbUserName, $this->dbPass, $this->DBDatabase);
	if (!$dbh)
    	{
        //die("Not connected : " . mysqli_error($dbh));
	return "db_connect_failure";
    	}
	
	$query = "SELECT first_name,last_name,customer_key FROM `$this->dbDatabase`.`$this->dbTable` ORDER BY '$orderby' DESC";
	echo $query . "<p>"; 
	$result = mysqli_query($dbh,$query);
		
		while ($row = mysqli_fetch_assoc($result)) {
			$returnArray[] = array_merge($row);
		}
		
		if (!$result) {
			if ($DEBUG) { echo mysqli_error($dbh); }
			mysqli_close($dbh); 
			return ("user_listing_failed");
			
		}
		
		print_r($returnArray);
		
		mysqli_close($dbh); 
		return ($returnArray);
		
} // End CustomerList

public function displayUserListPlainText() {
	$userListArray = $this->getCustomerList("first_name");
	
	
	
	foreach($userListArray as $element) {
		echo $element["first_name"];
		echo "&nbsp;";
		echo $element["last_name"];
		echo "&nbsp;";
		echo $element["customer_key"];
		echo "<br>";
	}
	
}

*/	

} // End of Class Customer




?>
